{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.10. Interacting with asynchronous parallel tasks in IPython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import time\n",
    "import ipyparallel\n",
    "import ipywidgets\n",
    "from IPython.display import clear_output, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "rc = ipyparallel.Client()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "view = rc.load_balanced_view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    import time\n",
    "    time.sleep(.1)\n",
    "    return x * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = list(range(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ar = view.map_async(f, numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'after': None,\n",
       " 'completed': None,\n",
       " 'data': {},\n",
       " ...\n",
       " 'submitted': datetime.datetime(2017, ...)}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar.metadata[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0, 1, 4, ..., 9801,\n"
     ]
    }
   ],
   "source": [
    "for i in ar:\n",
    "    print(i, end=', ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def progress_bar(ar):\n",
    "    # We create a progress bar.\n",
    "    w = ipywidgets.IntProgress()\n",
    "    # The maximum value is the number of tasks.\n",
    "    w.max = len(ar.msg_ids)\n",
    "    # We display the widget in the output area.\n",
    "    display(w)\n",
    "    # Repeat:\n",
    "    while not ar.ready():\n",
    "        # Update the widget's value with the\n",
    "        # number of tasks that have finished\n",
    "        # so far.\n",
    "        w.value = ar.progress\n",
    "        time.sleep(.1)\n",
    "    w.value = w.max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ar = view.map_async(f, numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "podoc": {
     "output_text": "Progress bar"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABPgAAACXCAYAAACIquUGAAAAAXNSR0IArs4c6QAAAARnQU1BAACx\njwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAA1KSURBVHhe7d3LbhxZAcfhvBV7nsF7hofgBfAK\nzcADDLDxCsSWGbbeMguSbBIuipLgJMrVueDEUS4qcqrrOG3nVNepW7tO+vtJn0aMx91ddhu5/qpu\nX6kkSZIkSZIkFZuBT5IkSZIkSSo4A58kSZIkSZJUcAY+SZIkSZIkqeAMfJIkSZIkSVLBTTLwvX37\ntjo6Oqpu3rxZXb16FQAAAADoELa0sKmFbW1Mowe+8CBSDxAAAAAAyBM2tqGNGvjWr9i7c+dOdXx8\nXJ2engIAAAAAHcKWduvWrbN9LWxtQxo88MUr965du2bYAwAAAICBwrYWNrawtQ25km/QwBdeFxyX\nReMeAAAAAIwTNra4t/V9T75BA1+8ei+8LDf1gAAAAACAfuLLdftexTdo4IvvvefqPQAAAACYRryK\nr+978Q0a+MIdBakHAgAAAAAME3e3Phn4AAAAAGAhDHwAAAAAUDADHwAAAAAUzMAHAAAAAAUz8AEA\nAABAwQx8AAAAAFAwAx8AAAAAFMzABwAAAAAFM/ABAAAAQMEMfAAAAABQMAMfAAAAABTMwAcAAAAA\nBTP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q5BxgDAOfJEmaIwMfALRInZwDjGHgkyRJc2TgA4AWqZNzgDEMfJIkaY4MfADQInVyDjCG\ngU+SJM2RgQ8AWqROzgHGMPBJkqQ5MvABQIvUyTnAGAY+SZI0RwY+AGiROjkHGMPAJ0mS5sjABwAt\nUifnAGMY+CRJ0hwZ+ACgRerkHGAMA58kSZojAx8AtEidnAOMYeCTJElzZOADgBapk3OAMQx8kiRp\njgx8ANAidXIOMIaBT5IkzZGBDwBapE7OAcYw8EmSpDky8AFAi9TJOcAYBj5JkjRHBj4AaJE6OQcY\nw8AnSZLmyMAHAC1SJ+cAYxj4JEnSHC1y4PvmD1eTJ1oAsE2pk3OAMV68eFEdHx83v/VKkiRN0yIH\nvt/++O/kiRYAbFPq5BxgjDDwvXr1qvmtV5IkaZoWOfAdPX2dPNECgG1KnZwDDHVyclI9f/68+vDh\nQ/NbryRJ0jQtcuAL/e5v/0mebAHAtqRO0AGGClfuhZFPkiRp6hY78IV++cdryRMuANiG1Ak6wBBh\n2PPSXEmSNFeLHvhCv/7T36uf/eovALB1qZN0gL7CuPfmzZvmt1tJkqTpW/zAF7r98GW1/+efqr3f\n/Jg8AQOAOaRO1AH6OD09rT5+/Nj8VitJkjRPRQx8kiRJkiRJktIZ+CRJkiRJkqSCM/BJkiRJkiRJ\nBWfgkyRJkiRJkgrOwCdJkiRJkiQVnIFPkiRJkiRJKjgDnyRJkiRJklRwBj5JkiRJkiSp4Ax8kiRJ\nkiRJUsEZ+CRJkiRJkqSCM/BJkiRJkiRJBWfgkyRJkiRJkgrOwCdJkiRJkiQVnIFPkiRJkiRJKjgD\nnyRJkiRJklRsVfV/hS/rOJyG5vwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "Progress bar"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "progress_bar(ar)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
